package newcrunch.util;

import java.awt.Color;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;
import javax.swing.ImageIcon;
import jsyntaxpane.util.JarServiceProvider;

/**
 * Class to load settings from property files
 * @author WL
 */
public class PropertyLoader 
{
    private static String colorsFilename = "newcrunch/display/Color.properties";
    private static Properties colorProps = new Properties();
    
    private static String stringsFilename = "newcrunch/display/Text.properties";
    private static Properties stringsProps = new Properties();
    
    private static String connectionsFilename = "newcrunch/connection/Connection.properties";
    private static Properties connectionProps = new Properties();
    
    private static String imagesFilename = "newcrunch/images/images.properties";
    private static Properties imagesProps = new Properties();
    
    private static String generalFilename = "newcrunch/general.properties";
    private static Properties generalProps = new Properties();
    
    private static String rulesFilename = "newcrunch/codeanalysis/rules.properties";
    private static Properties rulesProps = new Properties();
    
    protected PropertyLoader() {}
    
    static
    {
        try {
            colorProps.load(JarServiceProvider.findResource(colorsFilename));
            
            stringsProps.load(JarServiceProvider.findResource(stringsFilename));
            
            connectionProps.load(JarServiceProvider.findResource(connectionsFilename));
            
            imagesProps.load(JarServiceProvider.findResource(imagesFilename));
            
            generalProps.load(JarServiceProvider.findResource(generalFilename));
            
            rulesProps.load(JarServiceProvider.findResource(rulesFilename));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public static String getLibraryPath()
    {
        return generalProps.getProperty("libraryPath");
    }
    
    public static Color getColorProperty(String property)
    {
        String[] color = colorProps.getProperty(property).split("\\s*,\\s*");
        return new Color(Integer.parseInt(color[0]), Integer.parseInt(color[1]), Integer.parseInt(color[2]));
    }
    
    public static String getStringProperty(String property)
    {
        return stringsProps.getProperty(property);
    }
    
    public static String getConnectionProperty(String property)
    {
        return connectionProps.getProperty(property);
    }
    
    public static String getRulesProperty(String property)
    {
        return rulesProps.getProperty(property);
    }
    
    private static ClassLoader getClassLoader()
    {
        ClassLoader loader = PropertyLoader.class.getClassLoader();
        return loader == null ? ClassLoader.getSystemClassLoader() : loader;
    }
    
    public static URL findResourceURL(String url)
    {
        URL loc = getClassLoader().getResource(url);
        
        return loc != null ? loc : ClassLoader.getSystemResource(imagesProps.getProperty(url));
    }
    
    public static ImageIcon getImageIcon(String property) 
    {
        URL resource = findResourceURL(imagesProps.getProperty(property));
        return new ImageIcon(resource);
    }
}
